package com.sigmazero13.gameutils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.sigmazero13.gameutils.model.GameInfo;

public class GameInfoCustomEdit extends Activity {
	
	private EditText min;
	private EditText max;
	private GameInfo game;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_info_custom);
		
		Bundle bundle = getIntent().getExtras();
		Integer gameId = bundle.getInt("game_id", 0);
		
		game = ((BoardGameUtilsApp)getApplication()).getDbHelper().selectSingleGame(gameId);
		
		min = (EditText)findViewById(R.id.custom_min_players);
		min.setHint(game.getMinPlayers(false).toString());
		
		max = (EditText)findViewById(R.id.custom_max_players);
		max.setHint(game.getMaxPlayers(false).toString());
		
		if (game.getCustomInfo() != null) {
			min.setText(game.getCustomInfo().getMinPlayers().toString());
			max.setText(game.getCustomInfo().getMaxPlayers().toString());
		}
		
		Button save = (Button)findViewById(R.id.custom_save_button);
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				Integer minP = null;
				Integer maxP = null;
				try {
					minP = Integer.parseInt(min.getText().toString());
				} catch (NumberFormatException nfe) {}
				try {
					maxP = Integer.parseInt(max.getText().toString());
				} catch (NumberFormatException nfe) {}
				
				i.putExtra("min_players",minP);
				i.putExtra("max_players",maxP);
				setResult(RESULT_OK, i);
				finish();
			}
		});
	}
}
